package com.learn.pass;

import java.util.HashMap;
import java.util.Map;

public class TwoSum {
    public int[] twoSum(int[] nums, int target) {
        // 思路是使用map 对应的 key=target-value、value
        int[] res=new int[2];
        Map<Integer,Integer> store = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            // 计算对应的key ，value
            Integer need = target-nums[i];
            Integer key =nums[i];
            Integer value =i;
            // 找有没有需要的值
            Integer ortherIndex = store.get(need);
            if(ortherIndex !=null){
                res[0]=i;
                res[1]=ortherIndex;
                break;
            }
            store.put(key,value);
        }
        return res;
    }
}
